//
//  PhotoSelectCell.m
//  照片选择器
//
//  Created by WONG on 16/3/3.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "PhotoSelectCell.h"

@interface PhotoSelectCell ()

@property (nonatomic,strong) UIButton *addButton;

@property (nonatomic,strong) UIButton *removeButton;


@end


@implementation PhotoSelectCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
    [self.contentView addSubview:self.addButton];
    [self.contentView addSubview:self.removeButton];
    
    // 添加约束
    _addButton.translatesAutoresizingMaskIntoConstraints = NO;
    _removeButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSDictionary *viewsDic = @{@"ab": self.addButton, @"rb": self.removeButton};
    /*
        VFL
     */
    // addButton 填充父控件
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[ab]-0-|" options:0 metrics:nil views:viewsDic]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[ab]-0-|" options:0 metrics:nil views:viewsDic]];
    
    // removeButton 父控件右上角
    // 水平在父控件右边
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[rb]-0-|" options:0 metrics:nil views:viewsDic]];
    // 垂直在父控件顶部
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[rb]" options:0 metrics:nil views:viewsDic]];
}

- (void)setAddButton {
    [_addButton setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
    [_addButton setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
    self.removeButton.hidden = YES;
}

- (void)setImage:(UIImage *)image {
    _image = image;
    [self.addButton setImage:image forState:UIControlStateNormal];
    self.removeButton.hidden = NO;
}



- (void)addButtonClick:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(photoSelectCell:addButtonClick:)]) {
        [self.delegate photoSelectCell:self addButtonClick:button];
    }
}
- (void)removeButtonClick:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(photoSelectCell:removeButtonClick:)]) {
        [self.delegate photoSelectCell:self removeButtonClick:button];
    }
}

#pragma mark - lazy load

- (UIButton *)addButton {
    if (_addButton == nil) {
        _addButton = [[UIButton alloc] init];
        
        [_addButton setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [_addButton setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
        
        [_addButton.imageView setContentMode:UIViewContentModeScaleToFill];
        
        [_addButton addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (UIButton *)removeButton {
    if (_removeButton == nil) {
        _removeButton = [[UIButton alloc] init];
        [_removeButton setBackgroundImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
        [_removeButton addTarget:self action:@selector(removeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _removeButton.hidden = YES;
    }
    return _removeButton;
}



@end













